package com.hethongviet.vending.network;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;

import com.hethongviet.vending.Utils;
import com.hethongviet.vending.model.Account;
import com.hethongviet.vending.model.Box;
import com.hethongviet.vending.model.BoxInventory;
import com.hethongviet.vending.model.BoxItem;
import com.hethongviet.vending.model.MachineInfo;
import com.hethongviet.vending.model.ProductSell;
import com.hethongviet.vending.model.ProductService;
import com.hethongviet.vending.model.Transaction;
import com.hethongviet.vending.network.CONSTANTS.JSON;
import com.hethongviet.vending.network.CONSTANTS.SERVICE;
import com.hethongviet.vending.pages.GeneralActivity;

public class Network
{
	private static String username = CONFIG.NETWORK_USERNAME;
	private static String password = CONFIG.NETWORK_PASSWORD;
	private static String databaseServer = CONFIG.NETWORK_DATABASE_SERVER;
	private static final String CONTENT_TYPE = "application/json";

	private static GeneralActivity onNetworkHandleListener = null;

	private static Transaction transaction = null;

	public static void send(final String method, final String messageOut)
	{
		new Thread(new Runnable()
		{

			@Override
			public void run()
			{
				// Create a new HttpClient and Post Header

				HttpParams myParams = new BasicHttpParams();
				HttpConnectionParams.setConnectionTimeout(myParams, 10000);
				HttpConnectionParams.setSoTimeout(myParams, 10000);
				HttpClient httpclient = new DefaultHttpClient();
				String responseString = null;
				try
				{
					HttpPost httppost = new HttpPost(
							CONSTANTS.SERVICE.SERVER_URL + databaseServer + "/" + method + ".php");
					httppost.setHeader("Content-type", CONTENT_TYPE);
					StringEntity se = new StringEntity(messageOut);
					se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
							CONTENT_TYPE));
					httppost.setEntity(se);

					HttpResponse response = httpclient.execute(httppost);
					responseString = EntityUtils.toString(response
							.getEntity());
					
					responseString = reprocessMessage(responseString);
					
					Log.i("WEB response", responseString);

					if (transaction != null)
					{
						transaction.setMessageReceived(responseString);
					}
					
				} catch (Exception e)
				{
					e.printStackTrace();
					Log.e("WEB Http error", e.toString());

					if (getOnNetworkHandleListener() != null)
					{
						getOnNetworkHandleListener().onConnectFail(
								CONSTANTS.NETWORK_ERROR.CONNECT);
					}
					
					return;
				}
				

				if (getOnNetworkHandleListener() != null)
				{
					getOnNetworkHandleListener().onNetworkMessageIncomming(
							transaction);
				}
			}
		}).start();

	}

	private static String reprocessMessage(String message)
	{
		String messageTemp = "";
		
		for (int i = 0; i < message.length(); i++)
		{
			char c = message.charAt(i);
			
			if(c != '\n' && c != '\r')
			{
				messageTemp += c;
			}
		}
		
		return messageTemp.trim();
	}
	
	public static boolean sendProductSell(ProductSell productSell,
			MachineInfo machineInfo)
	{
		/* Create JSON */
		JSONObject jsonObject = new JSONObject();
		try
		{

			jsonObject.put(CONSTANTS.JSON.USERNAME, username);
			jsonObject.put(CONSTANTS.JSON.PASSWORD, password);

			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_ACCOUNT_NUMBER,
					productSell.getAccountNumber());
			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_ID,
					createId(productSell.getMachineCode()));
			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_BOX_CODE,
					productSell.getIdBox());
			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_PRODUCT_CODE,
					productSell.getCodeProduct());
			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_DATE,
					getDateString(productSell.getDate()));
			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_IS_DELIVERIED,
					productSell.isDelivered() ? "1" : "0");
			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_PRICE,
					productSell.getPrice());

			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_MACHINE_CODE,
					productSell.getMachineCode());
			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_WORK_NUMBER1,
					productSell.getWorkerNumber1());
			jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_WORK_NUMBER2,
					productSell.getWorkerNumber2());

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObject.toString());
		send(SERVICE.ADD_PRODUCT_SELLOUT, jsonObject.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObject.toString());
		transaction.setModel(productSell);
		transaction.setMethodSent(SERVICE.ADD_PRODUCT_SELLOUT);

		return true;
	}

	public static boolean sendMachineInfo(MachineInfo machine)
	{
		/* Create JSON */
		JSONObject jsonObject = new JSONObject();
		try
		{
			jsonObject.put(CONSTANTS.JSON.USERNAME, username);
			jsonObject.put(CONSTANTS.JSON.PASSWORD, password);
			jsonObject.put(CONSTANTS.MACHINE.COLUMN_AMOUNT_BOX,
					machine.getBoxAmount());
			jsonObject.put(CONSTANTS.MACHINE.COLUMN_CODE, machine.getCode());
			// jsonObject.put(CONSTANTS.MACHINE.COLUMN_DATE_CREATED,
			// machine.getDateCreated());
			// jsonObject.put(CONSTANTS.MACHINE.COLUMN_DATE_TERMINATED,
			// machine.getDateTerminated());
			jsonObject
					.put(CONSTANTS.MACHINE.COLUMN_GROUP, machine.getGroupId());
			// jsonObject.put(CONSTANTS.MACHINE.COLUMN_LATITTUDE,
			// machine.getLatitude());
			jsonObject.put(CONSTANTS.MACHINE.COLUMN_LOCATION_ID,
					machine.getLocationId());
			// jsonObject.put(CONSTANTS.MACHINE.COLUMN_LONGITUDE,
			// machine.getLongitude());
			// jsonObject.put(CONSTANTS.MACHINE.COLUMN_STAFF_CREATE,
			// machine.getStaffCreate());
			// jsonObject.put(CONSTANTS.MACHINE.COLUMN_STAFF_TERMINATE,
			// machine.getStaffTerminate());
			// jsonObject.put(CONSTANTS.MACHINE.COLUMN_STAFF_UPDATE,
			// machine.getStaffUpdate());
			// jsonObject.put(CONSTANTS.MACHINE.COLUMN_STATUS,
			// machine.getStatus());
			// jsonObject.put(CONSTANTS.MACHINE.COLUMN_STATUS_RENT,
			// machine.getStatusRent());

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObject.toString());
		send(SERVICE.UPDATE_MACHINE_INFO, jsonObject.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObject.toString());
		transaction.setModel(null);
		transaction.setMethodSent(SERVICE.UPDATE_MACHINE_INFO);

		return true;
	}

	public static boolean sendRequestBoxConfig(String machineCode)
	{

		JSONObject jsonObject = new JSONObject();
		try
		{
			jsonObject.put(CONSTANTS.JSON.USERNAME, username);
			jsonObject.put(CONSTANTS.JSON.PASSWORD, password);
			jsonObject.put(CONSTANTS.JSON.MACHINE_CODE, machineCode);

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObject.toString());
		send(SERVICE.GET_BOX_CONFIG, jsonObject.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObject.toString());
		transaction.setModel(null);
		transaction.setMethodSent(SERVICE.GET_BOX_CONFIG);

		return true;
	}

	public static boolean sendRequestUpdateAccount()
	{

		JSONObject jsonObject = new JSONObject();
		try
		{
			jsonObject.put(CONSTANTS.JSON.USERNAME, username);
			jsonObject.put(CONSTANTS.JSON.PASSWORD, password);
			jsonObject.put(CONSTANTS.JSON.TYPE, CONSTANTS.JSON.ACCOUNT);

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObject.toString());
		send(SERVICE.GET_ACCOUNTS, jsonObject.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObject.toString());
		transaction.setModel(null);
		transaction.setMethodSent(SERVICE.GET_ACCOUNTS);

		return true;
	}

	public static boolean sendRequestLogin(String machineCode, String id)
	{

		JSONObject jsonObject = new JSONObject();
		try
		{
			jsonObject.put(CONSTANTS.JSON.USERNAME, username);
			jsonObject.put(CONSTANTS.JSON.PASSWORD, password);
			jsonObject.put(CONSTANTS.JSON.ACCOUNT_ID, id);
			jsonObject.put(CONSTANTS.JSON.MACHINE_CODE, machineCode);

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObject.toString());
		send(SERVICE.LOGIN, jsonObject.toString());

		Account account = new Account(id);
		
		transaction = new Transaction();
		transaction.setMessageSent(jsonObject.toString());
		transaction.setModel(account);
		transaction.setMethodSent(SERVICE.LOGIN);

		return true;
	}

	public static boolean sendRequestUpdateProduct()
	{

		JSONObject jsonObject = new JSONObject();
		try
		{
			jsonObject.put(CONSTANTS.JSON.USERNAME, username);
			jsonObject.put(CONSTANTS.JSON.PASSWORD, password);
			jsonObject.put(CONSTANTS.JSON.TYPE, CONSTANTS.JSON.PRODUCT);

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObject.toString());
		send(SERVICE.GET_PRODUCTS, jsonObject.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObject.toString());
		transaction.setModel(null);
		transaction.setMethodSent(SERVICE.GET_PRODUCTS);

		return true;
	}

	public static boolean sendProductPutIn(ProductService productService)
	{
		/* Create JSON */
		JSONObject jsonObject = new JSONObject();
		try
		{

			jsonObject.put(CONSTANTS.JSON.USERNAME, username);
			jsonObject.put(CONSTANTS.JSON.PASSWORD, password);

			jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_ID,
					createId(productService.getMachineCode()));
			jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_AMOUNT,
					productService.getAmount());
			jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_BOX_CODE,
					productService.getIdBox());
			jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_DATE,
					getDateString(productService.getDate()));
			jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_MACHINE_CODE,
					productService.getMachineCode());
			jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_PRODUCT_CODE,
					productService.getCodeProduct());
			jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_STAFF_CHECKER,
					productService.getStaffChecker());
			jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_STAFF_SERVICE,
					productService.getStaffService());

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObject.toString());
		send(SERVICE.ADD_PRODUCT_PUTIN, jsonObject.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObject.toString());
		transaction.setModel(productService);
		transaction.setMethodSent(SERVICE.ADD_PRODUCT_PUTIN);

		return true;
	}

	public static boolean sendProductTakeOut(ProductService productService)
	{
		/* Create JSON */
		JSONObject jsonObject = new JSONObject();
		try
		{

			jsonObject.put(CONSTANTS.JSON.USERNAME, username);
			jsonObject.put(CONSTANTS.JSON.PASSWORD, password);

			jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_ID,
					createId(productService.getMachineCode()));
			jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_AMOUNT,
					productService.getAmount());
			jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_BOX_CODE,
					productService.getIdBox());
			jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_DATE,
					getDateString(productService.getDate()));
			jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_MACHINE_CODE,
					productService.getMachineCode());
			jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_PRODUCT_CODE,
					productService.getCodeProduct());
			jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_STAFF_CHECKER,
					productService.getStaffChecker());
			jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_STAFF_SERVICE,
					productService.getStaffService());

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObject.toString());
		send(SERVICE.ADD_PRODUCT_TAKEOUT, jsonObject.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObject.toString());
		transaction.setModel(productService);
		transaction.setMethodSent(SERVICE.ADD_PRODUCT_TAKEOUT);

		return true;
	}

	public static boolean sendProductChange(ProductService productService)
	{
		/* Create JSON */
		JSONObject jsonObject = new JSONObject();
		try
		{

			jsonObject.put(CONSTANTS.JSON.USERNAME, username);
			jsonObject.put(CONSTANTS.JSON.PASSWORD, password);

			jsonObject.put(CONSTANTS.PRODUCT_CHANGE.COLUMN_ID,
					createId(productService.getMachineCode()));
			jsonObject.put(CONSTANTS.PRODUCT_CHANGE.COLUMN_BOX_CODE,
					productService.getIdBox());
			jsonObject.put(CONSTANTS.PRODUCT_CHANGE.COLUMN_DATE,
					getDateString(productService.getDate()));
			jsonObject.put(CONSTANTS.PRODUCT_CHANGE.COLUMN_MACHINE_CODE,
					productService.getMachineCode());
			jsonObject.put(CONSTANTS.PRODUCT_CHANGE.COLUMN_PRODUCT_CODE,
					productService.getCodeProduct());
			jsonObject.put(CONSTANTS.PRODUCT_CHANGE.COLUMN_STAFF_CHECKER,
					productService.getStaffChecker());
			jsonObject.put(CONSTANTS.PRODUCT_CHANGE.COLUMN_STAFF_PUT,
					productService.getStaffService());

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObject.toString());
		send(SERVICE.ADD_PRODUCT_CHANGE, jsonObject.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObject.toString());
		transaction.setModel(productService);
		transaction.setMethodSent(SERVICE.ADD_PRODUCT_CHANGE);

		return true;
	}

	public static boolean sendBoxs(List<BoxItem> boxItems)
	{
		if (boxItems == null || boxItems.size() == 0) return true;

		JSONObject jsonObjectSend = new JSONObject();

		try
		{
			jsonObjectSend.put(CONSTANTS.JSON.USERNAME, username);
			jsonObjectSend.put(CONSTANTS.JSON.PASSWORD, password);

			JSONArray jsonArray = new JSONArray();

			for (BoxItem boxItem : boxItems)
			{
				Box box = boxItem.getBox();
				JSONObject jsonObject = new JSONObject();

				// jsonObject.put(
				// CONSTANTS.BOX.COLUMN_ACTIVE,
				// box.getActive());

				jsonObject.put(CONSTANTS.BOX.COLUMN_ID, box.getId());

				// jsonObject.put(
				// CONSTANTS.BOX.COLUMN_NAME,
				// box.getName());

				jsonObject.put(CONSTANTS.BOX.COLUMN_PRODUCT_AMOUNT,
						box.getProductAmount());

				jsonObject.put(CONSTANTS.BOX.COLUMN_PRODUCT_CODE,
						box.getProductCode());

				jsonObject.put(CONSTANTS.BOX.COLUMN_PRODUCT_PRICE,
						box.getProductPrice());

//				jsonObject.put(CONSTANTS.BOX.COLUMN_DATE_UPDATED,
//						getDateString(System.currentTimeMillis()));

				jsonArray.put(jsonObject);
			}

			jsonObjectSend.put(JSON.KEY_DATA_ARRAY, jsonArray);
		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */
		Utils.debug("Sending message = " + jsonObjectSend.toString());
		send(SERVICE.UPDATE_BOX, jsonObjectSend.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObjectSend.toString());
		transaction.setModel(null);
		transaction.setMethodSent(SERVICE.UPDATE_BOX);

		return true;
	}

	public static boolean sendDataToServer(List<ProductSell> productSells,
			List<ProductService> productServices,
			List<BoxInventory> boxInventories, List<Box> boxes)
	{
		JSONObject jsonObjectSend = new JSONObject();

		try
		{
			jsonObjectSend.put(CONSTANTS.JSON.USERNAME, username);
			jsonObjectSend.put(CONSTANTS.JSON.PASSWORD, password);
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}

		JSONArray jsonArraySend = new JSONArray();

		/* Process products sale out */
		/* Create JSON */
		JSONArray jsonArraySellOut = new JSONArray();
		try
		{
			for (ProductSell productSell : productSells)
			{
				JSONObject jsonObject = new JSONObject();

				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_ACCOUNT_NUMBER,
						productSell.getAccountNumber());
				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_AMOUNT, 1);
				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_BOX_CODE,
						productSell.getIdBox());
				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_DATE,
						productSell.getDate());
				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_IS_DELIVERIED,
						productSell.isDelivered());
				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_MACHINE_CODE,
						productSell.getMachineCode());
				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_PRICE,
						productSell.getPrice());
				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_PRODUCT_CODE,
						productSell.getCodeProduct());
				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_WORK_NUMBER1,
						productSell.getWorkerNumber1());
				jsonObject.put(CONSTANTS.PRODUCT_SELL.COLUMN_WORK_NUMBER2,
						productSell.getWorkerNumber2());

				jsonArraySellOut.put(jsonObject);
			}
			jsonArraySend.put(jsonArraySellOut);

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Process products product put in */
		/* Create JSON */
		JSONArray jsonArrayPutIn = new JSONArray();
		try
		{
			for (ProductService productService : productServices)
			{
				if (productService.getTransaction() == ProductService.TRANSACTION_PRODUCT_PUT_IN)
				{
					JSONObject jsonObject = new JSONObject();

					jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_AMOUNT,
							productService.getAmount());
					jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_BOX_CODE,
							productService.getIdBox());
					jsonObject.put(CONSTANTS.PRODUCT_PUT_IN.COLUMN_DATE,
							productService.getDate());
					jsonObject.put(
							CONSTANTS.PRODUCT_PUT_IN.COLUMN_MACHINE_CODE,
							productService.getMachineCode());
					jsonObject.put(
							CONSTANTS.PRODUCT_PUT_IN.COLUMN_PRODUCT_CODE,
							productService.getCodeProduct());
					jsonObject.put(
							CONSTANTS.PRODUCT_PUT_IN.COLUMN_STAFF_CHECKER,
							productService.getStaffChecker());
					jsonObject.put(
							CONSTANTS.PRODUCT_PUT_IN.COLUMN_STAFF_SERVICE,
							productService.getStaffService());

					jsonArrayPutIn.put(jsonObject);
				}
			}

			jsonArraySend.put(jsonArrayPutIn);

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Process products take out */
		/* Create JSON */
		JSONArray jsonArrayTakeOut = new JSONArray();
		try
		{
			for (ProductService productService : productServices)
			{

				if (productService.getTransaction() == ProductService.TRANSACTION_PRODUCT_TAKE_OUT)
				{
					JSONObject jsonObject = new JSONObject();

					jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_AMOUNT,
							productService.getAmount());
					jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_BOX_CODE,
							productService.getIdBox());
					jsonObject.put(CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_DATE,
							productService.getDate());
					jsonObject.put(
							CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_MACHINE_CODE,
							productService.getMachineCode());
					jsonObject.put(
							CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_PRODUCT_CODE,
							productService.getCodeProduct());
					jsonObject.put(
							CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_STAFF_CHECKER,
							productService.getStaffChecker());
					jsonObject.put(
							CONSTANTS.PRODUCT_TAKE_OUT.COLUMN_STAFF_SERVICE,
							productService.getStaffService());

					jsonArrayTakeOut.put(jsonObject);
				}
			}

			jsonArraySend.put(jsonArrayTakeOut);

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Process box inventory */
		/* Create JSON */
		JSONArray jsonArrayBoxInventory = new JSONArray();
		try
		{
			for (BoxInventory boxInventory : boxInventories)
			{

				JSONObject jsonObject = new JSONObject();

				jsonObject.put(CONSTANTS.BOX_INVENTORY.COLUMN_AMOUNT,
						boxInventory.getProductAmount());

				jsonObject.put(CONSTANTS.BOX_INVENTORY.COLUMN_BOX_CODE,
						boxInventory.getIdBox());

				jsonObject.put(CONSTANTS.BOX_INVENTORY.COLUMN_MACHINE_CODE,
						boxInventory.getMachineCode());

				jsonObject.put(CONSTANTS.BOX_INVENTORY.COLUMN_PRODUCT_CODE,
						boxInventory.getProductCode());
				jsonObject.put(CONSTANTS.BOX_INVENTORY.COLUMN_DATE,
						getDateString(boxInventory.getDate()));

				jsonArrayBoxInventory.put(jsonObject);
			}
			jsonArraySend.put(jsonArrayBoxInventory);

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Process box information */
		/* Create JSON */

		JSONArray jsonArrayBox = new JSONArray();
		try
		{
			for (Box box : boxes)
			{

				JSONObject jsonObject = new JSONObject();

				// jsonObject.put(
				// CONSTANTS.BOX.COLUMN_ACTIVE,
				// box.getActive());

				jsonObject.put(CONSTANTS.BOX.COLUMN_ID, box.getId());

				// jsonObject.put(
				// CONSTANTS.BOX.COLUMN_NAME,
				// box.getName());

				jsonObject.put(CONSTANTS.BOX.COLUMN_PRODUCT_AMOUNT,
						box.getProductAmount());

				jsonObject.put(CONSTANTS.BOX.COLUMN_PRODUCT_CODE,
						box.getProductCode());

				jsonObject.put(CONSTANTS.BOX.COLUMN_PRODUCT_PRICE,
						box.getProductPrice());

				// jsonObject.put(CONSTANTS.BOX.COLUMN_DATE_UPDATED,
				// getDateString(System.currentTimeMillis()));

				jsonArrayBox.put(jsonObject);
			}

			jsonArraySend.put(jsonArrayBox);

			/* Put all */
			jsonObjectSend.put(JSON.KEY_DATA_ARRAY, jsonArraySend);

		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}

		/* Send */

		Utils.debug("Sending message = " + jsonObjectSend.toString());
		send(SERVICE.UPDATE_ALL, jsonObjectSend.toString());

		transaction = new Transaction();
		transaction.setMessageSent(jsonObjectSend.toString());
		transaction.setModel(null);
		transaction.setMethodSent(SERVICE.UPDATE_ALL);

		return true;

	}

	public static String getUsername()
	{
		return username;
	}

	public static void setUsername(String _username)
	{
		username = _username;
	}

	public static String getPassword()
	{
		return password;
	}

	public static void setPassword(String _password)
	{
		password = _password;
	}

	private static String getDateString(long time)
	{
		Date date = new Date(time);
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

		return dateFormat.format(date);
	}

	private static String createId(String code)
	{
		return code + System.currentTimeMillis();
	}

	public static GeneralActivity getOnNetworkHandleListener()
	{
		return onNetworkHandleListener;
	}

	public static void setOnNetworkHandleListener(
			GeneralActivity onNetworkHandleListener)
	{
		Network.onNetworkHandleListener = onNetworkHandleListener;
	}

	public Transaction getTransaction()
	{
		return transaction;
	}

	public void setTransaction(Transaction transaction)
	{
		Network.transaction = transaction;
	}

	public static String getDatabaseServer()
	{
		return databaseServer;
	}

	public static void setDatabaseServer(String databaseServer)
	{
		Network.databaseServer = databaseServer;
	}
}
